package com.chinasoftinc.facommon.control.conversation;

import ohos.utils.zson.ZSONObject;

import com.huawei.ailife.service.kit.callback.DataCallback;
import com.huawei.ailife.service.kit.callback.DeviceListener;
import com.huawei.ailife.service.kit.constants.ApiParameter;
import com.huawei.ailife.service.kit.manager.DeviceManager;
import com.huawei.ailife.service.kit.model.ServiceEntity;

import com.chinasoftinc.facommon.DeviceControlConstants;
import com.chinasoftinc.facommon.utils.LogUtil;

import java.util.ArrayList;
import java.util.List;
import java.util.Objects;

/**
 * WIFI会话
 *
 * @since 2022-04-20
 */
public class WifiConversation extends Conversation {
    private static final String TAG = "WifiConversation";
    private DeviceManager deviceManager;
    private String deviceId;
    private DeviceListener deviceListener;

    public WifiConversation(DeviceManager deviceManager, String deviceId) {
        this.deviceManager = deviceManager;
        this.deviceId = deviceId;
    }

    @Override
    public void connect() {
        hiLinkDevice.getProfileData(ApiParameter.Source.FROM_CLOUD, new ArrayList<>(), new DataCallback<List<ServiceEntity>>() {
            @Override
            public void onSuccess(List<ServiceEntity> serviceEntities) {
                dataChangeCallback
                    .ifPresent(hiLinkDataCallback -> hiLinkDataCallback.onSuccess(DeviceControlConstants.DATA_TYPE_WIFI_GET_PROFILE_DATA, ZSONObject.toZSONString(serviceEntities)))
                    .noPresent(WarnLog.INSTANCE);
            }

            @Override
            public void onFailure(int code, String msg) {
                dataChangeCallback
                    .ifPresent(hiLinkDataCallback -> hiLinkDataCallback.onFail(DeviceControlConstants.DATA_TYPE_WIFI_GET_PROFILE_DATA, code, msg))
                    .noPresent(WarnLog.INSTANCE);
            }
        });
    }

    @Override
    public void disconnect() {
        // ignore
    }

    @Override
    public void onDeviceIdChange() {
        super.onDeviceIdChange();
        if (deviceManager != null && deviceListener != null) {
            try {
                deviceManager.unsubscribeDeviceEvent(deviceListener);
                deviceListener = null;
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    }

    @Override
    public void init() {
        onDeviceIdChange();
        LogUtil.info(TAG, "subscribeDeviceEvent start deviceId: " + deviceId);
        List<String> listParam = new ArrayList<>();
        listParam.add(deviceId);
        deviceListener = new DeviceListener.Builder()
            .addDeviceAddListener(hiLinkDevice -> {
                LogUtil.info(TAG, "subscribeDeviceEvent DeviceAddListener = "
                    + ZSONObject.toZSONString(hiLinkDevice));
                dataChangeCallback
                    .ifPresent(hiLinkDataCallback -> hiLinkDataCallback.onSuccess(DeviceControlConstants.DEVICE_ADD_LISTENER, ""))
                    .noPresent(WarnLog.INSTANCE);
            })
            .addDeviceDeleteListener(s -> {
                LogUtil.info(TAG, "subscribeDeviceEvent DeviceDeleteListener = " + s);
                dataChangeCallback
                    .ifPresent(hiLinkDataCallback -> hiLinkDataCallback.onSuccess(DeviceControlConstants.DEVICE_DELETE_LISTENER, s))
                    .noPresent(WarnLog.INSTANCE);
            })
            .addDeviceAttachListener(list -> {
                LogUtil.info(TAG, "subscribeDeviceEvent DeviceAttachListener = " + ZSONObject.toZSONString(list));
                dataChangeCallback
                    .ifPresent(hiLinkDataCallback -> hiLinkDataCallback.onSuccess(DeviceControlConstants.DEVICE_ATTACH_LISTENER, ZSONObject.toZSONString(list)))
                    .noPresent(WarnLog.INSTANCE);
            })
            .addDeviceInfoListener(listParam, (s, hiLinkDevice) -> {
                LogUtil.info(TAG, "subscribeDeviceEvent DeviceInfoListener = " + s + System.lineSeparator()
                    + ZSONObject.toZSONString(hiLinkDevice));
                dataChangeCallback
                    .ifPresent(hiLinkDataCallback -> hiLinkDataCallback.onSuccess(DeviceControlConstants.DEVICE_INFO_LISTENER, ZSONObject.toZSONString(hiLinkDevice)))
                    .noPresent(WarnLog.INSTANCE);
            })
            .addProfileDataListener(deviceId, profileDataChangeEntity -> {
                LogUtil.info(TAG, "subscribeDeviceEvent ProfileDataListener = "
                    + ZSONObject.toZSONString(profileDataChangeEntity));
                dataChangeCallback
                    .ifPresent(hiLinkDataCallback -> hiLinkDataCallback.onSuccess(DeviceControlConstants.PROFILE_DATA_LISTENER, ZSONObject.toZSONString(profileDataChangeEntity)))
                    .noPresent(WarnLog.INSTANCE);
            })
            .build();
        Objects.requireNonNull(deviceManager).subscribeDeviceEvent(deviceListener);
    }
}
